package vidudemnguocthoigian;



import java.util.Timer;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import vidudemnguocthoigian.ClockDemNguoc;


public class MyDemNguocThoiGianCanvas extends GameCanvas implements Runnable {
	//
	private boolean isRun = false;
	private ClockDemNguoc clockDemNguoc;
	//
	public MyDemNguocThoiGianCanvas() {
		super(true);
	}
	
	public void start(){
		clockDemNguoc = new ClockDemNguoc(60);
		new Timer().scheduleAtFixedRate(clockDemNguoc, 0, 1000);//Chay. sau 1s
		Thread thread = new Thread(this);
		thread.start();
	}
	
	public void run() {
		Graphics graphics = this.getGraphics();
		while (!isRun) {
			this.kiemtraTrangThaiUngDung();
			this.capnhapLaiManHinhUngDung(graphics);
		}
	}

	private void capnhapLaiManHinhUngDung(Graphics graphics) {
		// TODO Auto-generated method stub
		graphics.setColor(0xffffff);
		graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
		graphics.setColor(0xff0000);
		graphics.drawString("So thoi gian con lai :" + clockDemNguoc.tinhThoiGianConlai(), 0, 0, 0);
		flushGraphics();
	}

	private void kiemtraTrangThaiUngDung() {
		// TODO Auto-generated method stub
		if(clockDemNguoc.tinhThoiGianConlai() == 0){
			this.isRun = true;
		}
	}

}
